package com.opo5.logisticsmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.dao.DictDao;
import com.opo5.logisticsmanagement.domain.Dict;
import com.opo5.logisticsmanagement.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author lixin
* @description 针对表【tbs_dict】的数据库操作Service实现
* @createDate 2022-07-10 08:55:41
*/
@Service
public class DictServiceImpl extends ServiceImpl<DictDao, Dict>
    implements DictService {

    @Autowired
    private DictDao dictDao;

    @Override
    public R getDictByCondition(String text, String value, Integer currentPage, Integer pageSize,Boolean flag) {
        Page<Dict> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(text!=null,"text",text);
        queryWrapper.like(value!=null,"value",value);
        if (flag){
            queryWrapper.ne("status","0");
        }else{
            queryWrapper.ne("status","1");
        }

        dictDao.selectPage(page,queryWrapper);
        List<Dict> dicts = page.getRecords();
        return R.success(new R().add("dicts",dicts).add("total",page.getTotal()).getMap());
    }

    @Override
    public R getDictById(String id) {
        Dict dict = dictDao.selectById(id);
        return R.success(dict);
    }

    @Override
    public R addDict(Dict dict) {
        int flag = dictDao.insert(dict);
        if (flag==1){
            return R.success(null);
        }
        return R.error("添加字典信息失败");
    }

    @Override
    public R editDict(Dict dict) {
        int flag = dictDao.updateById(dict);
        if (flag==1){
            return R.success(null);
        }
        return R.error("修改字典信息失败");
    }

    @Override
    public R delDict(Integer id) {
        int i = dictDao.deleteById(id);
        if (i== 1){
            return R.success(null);
        }
        return R.error("删除失败");
    }

    @Override
    public R switchDict(Integer id) {
        Dict dict = dictDao.selectById(id);
        dict.setStatus(dict.getStatus().equals("1") ? "0" : "1");
        dictDao.updateById(dict);
        return R.success(null);
    }
}




